package com.team.easybuy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.easybuy.entity.User;
import com.team.easybuy.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/editUser")
    public boolean editUser(@RequestBody User user, HttpServletRequest req, HttpServletResponse resp) {
        return userService.editUser(user, req, resp);
    }

    @RequestMapping("/checkUser")
    public User checkUser(@RequestBody Map<String, Object> params) {
        return userService.findByNameAndPwd(params);
    }

    @RequestMapping("/updatePwd")
    public boolean updatePwd(@RequestBody Map<String, Object> params, HttpServletRequest req, HttpServletResponse resp) {
        return userService.editPwd(params, req, resp);
    }

    @RequestMapping("/PageByAllUser")
    public PageInfo<User> PageByAllUser(@RequestParam Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.pageByUser(params);
        return new PageInfo<>(userList, 5);
    }

    @RequestMapping("/loadUser/{id}")
    public User loadUser(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @RequestMapping("/editUserType")
    public boolean editUserType(@RequestBody User user) {
        return userService.editUserType(user);
    }

    @RequestMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return userService.delUser(id);
    }
}
